/**
 * yiqihao(一起好商务股份有限公司) copyright 
 */
package com.p2p.backend.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.p2p.base.annotation.LoginNeedless;
import com.p2p.base.model.GlobalConfig;
import com.p2p.base.result.ImgUploadResult;
import com.p2p.base.util.ImgUploadUtil;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.model.ImageStore;
import com.p2p.front.enums.PhotoType;
import com.p2p.front.service.ImageStoreService;

/**
 *
 * @author leiruiqi@yiqihao.com
 *
 * 2015年3月16日 下午4:48:55
 */

@LoginNeedless
@RequestMapping("/imageStore")
@Controller
public class ImageStoreController extends BaseController{

	@Resource
	private GlobalConfig globalConfig;
	
	@Resource
	private ImageStoreService imageStoreService;
	
	
	@RequestMapping("/uploadPage")
	public String uploadPage(){
		return "imageStore/uploadPage";
	}
	
	@RequestMapping("/mainPicStore")
	public String mainPicStore(){
		return "imageStore/mainPicStore";
	}
	
	@RequestMapping("/listImageStoreByBiz")
	public String listImageStoreByBiz(Long bizId,Integer bizType,Model model){
		
		List<ImageStore> list = imageStoreService.queryBizAndStatus(bizId, bizType,null);
		
		Map<String, Object> data = new HashMap<String, Object>();
		model.addAttribute("data", data);
		model.addAttribute("res", 1);
		data.put("rows", list);
		
		return "imageStore/list";
	}
	@RequestMapping("/editImageStoreUrl")
	public void editImageStoreUrl(Long id,String url,Model model){
		imageStoreService.updateUrl(id, url);
		model.addAttribute("res", 1);
	}
	/*@RequestMapping("/editImage")
	public void editImage(){
		imageStoreService.updateSortNo(id, sortNo)
	}*/
	@RequestMapping("/editImageStoreStatus")
	public void editImageStoreStatus(Long id,Integer status,Model model){
		imageStoreService.updateStatus(id, status);
		model.addAttribute("res", 1);
	}
	
	@RequestMapping("/editImageStoreSortNo")
	public void editImageStoreSortNo(Long id,Integer sortNo,Model model){
		imageStoreService.updateSortNo(id, sortNo);
		model.addAttribute("res", 1);
	}
	
	
	@RequestMapping("/editImageStoreSortNoAndStatus")
	public void editImageStoreSortNoAndStatus(Long id,Integer sortNo,Integer status,Model model){
		imageStoreService.updateSortNo(id, sortNo);
		imageStoreService.updateStatus(id, status);
		model.addAttribute("res", 1);
	}
	
	@RequestMapping("/editImageStoreSortNoAndStatusAndUrl")
	public void editImageStoreSortNoAndStatus(Long id,Integer sortNo,Integer status,String url,Model model){
		imageStoreService.updateSortNo(id, sortNo);
		imageStoreService.updateStatus(id, status);
		imageStoreService.updateUrl(id, url);
		model.addAttribute("res", 1);
	}

	@RequestMapping("/mainImageUpload")
	public void idCardImageUpload(HttpServletRequest request,
			HttpServletResponse response) throws IllegalStateException,
			IOException {
		ImgUploadResult result = ImgUploadUtil.saveUploadFile(request,
				globalConfig.getMainImageUploadPath());
		
		if (result.isSuccess()) {
			for (Entry<String, String> filenameInfo : result.getSuccessFinalFileNameInfoList().entrySet()) {
				imageStoreService.createDefault(filenameInfo.getKey(), 1L,
						PhotoType.SYS_CONFIG,filenameInfo.getValue());
			}
		}

		JSONObject jsonObj = new JSONObject();
		response.setContentType("text/html;charset=UTF-8");
		jsonObj.put("success", true);
		jsonObj.put("fileNames", result.getSuccessFinalFileNameInfoList());
		returnJsonObject(response, jsonObj);

	}
}
